package severlet;



import bean.Student;
import dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "insert",value = "/studentinsert")
public class StudentInsert extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取前端数据
        String studentid = req.getParameter("number");
        String studentname = req.getParameter("stunam");
        String major = req.getParameter("mayor");
        String gender = req.getParameter("sex");
        String department = req.getParameter("college");
        String classes = req.getParameter("class");
        String dormitoryid = req.getParameter("home");
        String phoneid = req.getParameter("phone");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date entrytime = null;
        try {
            entrytime = sdf.parse(req.getParameter("time"));
        } catch (ParseException e) {
            System.out.println("时间格式必须为0000-00-00");
            e.printStackTrace();
        }
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        resp.setCharacterEncoding("UTF-8");
//封装
        Student stu=new Student();
        stu.setStudentid(studentid);
        stu.setStudentname(studentname);
        stu.setMajor(major);
        stu.setGender(gender);
        stu.setDepartment(department);
        stu.setClasses(classes);
        stu.setDormitoryid(dormitoryid);
        stu.setPhoneid(phoneid);
        stu.setEntrytime(entrytime);
        stu.setUsername(username);
        stu.setPassword(password);

//调用dao层
        StudentDao studentDao=new StudentDao();

       try {
           studentDao.insertSQB(stu);
            System.out.println("添加成功");

           resp.getWriter().println("注册成功");
           req.getRequestDispatcher("sussefulLogin.jsp").forward(req,resp);
        }catch (Exception e){
       e.printStackTrace();

        }

    }
}